package com.gy.common.model.log;

import com.gy.common.market.LogName;
import com.gy.common.model.vo.SysDictVo;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public interface LogMark {
    default Map<String, Object> getLogName(List<SysDictVo> sysDictVos) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = this.getClass().getDeclaredFields();
        for (int j = 0; j < fields.length; j++) {
            Field field = fields[j];
            field.setAccessible(true);
            LogName annotation = field.getAnnotation(LogName.class);
            if (annotation != null) {
                if(annotation.isMain()){
                    map.put(annotation.name(), field.get(this));

                    map.put(annotation.name()+"___main", field.get(this));
                }else  if (StringUtils.isEmpty(annotation.select())) {
                    map.put(annotation.name(), field.get(this));
                }else   {
                    for (SysDictVo sysDictVo : sysDictVos) {
                        if (sysDictVo.getDictType().equals(annotation.select()) && Objects.equals(field.get(this), sysDictVo.getDictId())) {
                            map.put(annotation.name(), sysDictVo.getDictName());
                        }
                    }
                }
            }
        }
        return map;
    }
}
